<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>My Vue App</title>
  <script src="./vue-rels/vue2.js"></script>
  <!--必须在vue2.js后引入element-ui.js -->
  <!-- <script src="./vue-rels/element-ui.js"></script> -->
  <script src="api.js"></script>
  <script src="./vue-rels/sub_table_component.js"></script>
  <!-- <link rel="stylesheet" href="./vue-rels/element-ui.css"> -->
  <link rel="stylesheet" href="./vue-rels/sub_table.css"> 
  <style>
  
  </style>
</head>
<body>
  <div id="app">
    {{ message }}
    {{xx}}        

    <!--子表格组件-->
    <sub-table :tabledata="tableDatas">
        <template v-slot:header>
            <tr><th rowspan="2">名称</th><th rowspan="1" :colspan="3">其他信息</th></tr>
            <tr><th rowspan="1">类型</th><th rowspan="1">级别</th><th rowspan="1">法人</th> <th v-if="pageMode=='edit'">操作</th></tr>
        </template>
        
    </sub-table>

  <hr/>
  <!--子表格组件2-->
    <other-sub-table :tabledata="tableDatas2">
    </other-sub-table>

  </br>
  </br>
    
    <div>
        <button @click="submitData()">提交更改</button> <button @click="insertNewRecord()">新增一行</button> <button @click="showData()">查看数据</button>
    </div>

  </div>

  <script>
    // 申明子表格组件
    //["名称","类型","级别","法人"]
    var pageMode='edit'
    var config=[{title:"名称",field:"hospitalName",content:"<textarea></textarea>"},
                {title:"类型",field:"hospitalType",content:'<select ><option value="1">公立</option><option value="2">私立</option></select>'},
                {title:"级别",field:"hospitalLevel"},
                {title:"法人",field:"legalPerson"}]
    //私有导入并且创建组件实例1            
    var subTableComponent=getSubTableComponent(config,pageMode)

    var config2=[{title:"名称2",field:"hospitalName"},
                {title:"类型2",field:"hospitalType"},
                {title:"级别2",field:"hospitalLevel"},
                {title:"法人2",field:"legalPerson"}]
    //私有导入并且创建组件实例2    
    var otherSubTableComponent=getSubTableComponent(config2,pageMode)


    //构建vue实例
    var app = new Vue({
      el: '#app',
      data:function() {
        return {
        pageMode:pageMode,    
        message: 'Hello Vue!',
        xx:'',
        tableDatas:[{hospitalName:"测试数据1",hospitalType:"1",hospitalLevel:"1",legalPerson:"王"},{hospitalName:"测试数据2",hospitalType:"1",hospitalLevel:"1",legalPerson:"小马"}],
        tableDatas2:[{hospitalName:"测试数据3",hospitalType:"1",hospitalLevel:"1",legalPerson:"李"},{hospitalName:"测试数据4",hospitalType:"1",hospitalLevel:"1",legalPerson:"大赛"}],
      }
    },
      components:{
        "sub-table":subTableComponent,
        "other-sub-table":otherSubTableComponent
      },
      methods: {
        insertNewRecord(){
          app.tableDatas.push({hospitalName:"",hospitalType:"1",hospitalLevel:"",legalPerson:"暂无",operType:"2"})
        },
        showData(){
          console.log(app.tableDatas)
        }
      },
    });

    //异步初始化数据
    getTableData('共通表格查询',{},function(data){
      app.tableDatas=data.data;
      app.tableDatas2=JSON.parse(JSON.stringify(data.data))
    });

    //提交变更数据
    function submitData(){
        submitChangeData('共通表格编辑',{data:app.tableDatas},function(data){
            console.log("rs",data)
            if(data.code==0){
                alert('提交成功');
            }else{
                alert('提交失败');
            }
        })
    }   
    
   
  </script>
</body>
</html>